home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-28 | 10.1 KB | 476 lines | [TEXT/MPS ] |
- /*
- File: CAttribute.cp
-
- Contains: xxx put contents here xxx
-
- Written by: Tim Harnett
-
- Copyright: © 1994 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- <7> 3/6/95 TMH adapt to use Symantic compiler
- <6> 2/27/95 TMH adapt to use ETO16 universal headers
- <5> 2/21/95 TMH metrowerks changes, add explict cast
- <4> 2/14/95 CL remove segment
- <3> 2/14/95 CL remove globls
- <2> 12/12/94 TMH eliminate compiler warning
- <1> 9/20/94 TMH Abandon RoadsideRest embrace Mercury
- 4/5/94 TMH xxx put comment here xxx
-
- To Do:
- */
-
-
- #ifndef __OCEAUTHDIR__
- #include "OCEAuthDir.h"
- #endif
-
- #ifndef __OCEErrors__
- #include "OCEErrors.h"
- #endif
-
- #ifndef __BaseTypes__
- #include "BaseTypes.h"
- #endif
-
-
- #ifndef _Globals__
- #include "Globals.h"
- #endif
-
-
- #ifndef __CAttribute__
- #include "CAttribute.h"
- #endif
-
- #ifndef __CRecordID__
- #include "CRecordID.h"
- #endif
-
-
-
-
- //----------------------------------------------------------------------------------------
- // C A t t r i b u t e
- //----------------------------------------------------------------------------------------
-
- CAttribute::CAttribute(AttributeTypePtr attrType, AttributeTag tag, CreationID *cid)
- {
- fAttributeType = (const RString*)attrType; // copy
- fTag = tag;
- if (cid) {
- fCID.source = cid->source;
- fCID.seq = cid->seq;
- } else
- fCID.source = fCID.seq = 0;
-
- fDataLength = 0;
- fBytes = 0;
-
- fValueCallBack = 0;
- }
-
-
- //------------------------------------------------------------------------------------------
- CAttribute::CAttribute(short attrTypeIndex, AttributeTag tag, CreationID *cid)
- {
-
- fAttributeType = (RString*)OCEGetIndAttributeType(attrTypeIndex); // copy
- fTag = tag;
- if (cid)
- OCECopyCreationID(cid, &fCID);
- else
- fCID.source = fCID.seq = 0;
-
- fDataLength = 0;
- fBytes = 0;
-
- fValueCallBack = 0;
-
- }
-
-
- //------------------------------------------------------------------------------------------
- CAttribute::~CAttribute()
- {
- if( fBytes != 0 ) {
- DisposePtr((Ptr)fBytes);
- fBytes= 0;
- }
-
- }
-
- //------------------------------------------------------------------------------------------
- #pragma segment Templates
- OSErr CAttribute::Write1Value(CRecordID& rid,void* valueBuf, long valueBufSize, AuthIdentity identity)
- {
-
- DirAddAttributeValuePB pb;
-
- pb.dsRefNum = rid.GetDSRefNum();
- pb.identity = identity;
-
- NilAddrBlock(&pb.serverHint);
-
- pb.aRecord = rid;
-
- fBytes = valueBuf;
- fDataLength = valueBufSize;
- pb.attr = (Attribute*)this;
-
- OSErr osErr = DirAddAttributeValue((DirParamBlockPtr)&pb, false);
-
- fBytes = 0; // so destructor does not Dispose
- return osErr;
-
- }
-
-
- //------------------------------------------------------------------------------------------
- #pragma segment Templates
- OSErr CAttribute::AddAttribute(CRecordID& rid,AddrBlock *serverHint, AuthIdentity identity)
- {
-
- DirAddAttributeValuePB pb;
-
- pb.dsRefNum = rid.GetDSRefNum();;
- pb.identity = identity;
-
- NilAddrBlock(&pb.serverHint);
-
- pb.aRecord = rid;
-
- pb.attr = (Attribute*)this;
-
- OSErr osErr = DirAddAttributeValue((DirParamBlockPtr)&pb, false);
-
- return osErr;
-
- }
-
-
-
-
-
- //------------------------------------------------------------------------------------------
- #pragma segment Templates
- OSErr CAttribute::ChangeValue(CRecordID& rid,AddrBlock *serverHint, AuthIdentity identity)
- {
- DirChangeAttributeValuePB pb;
-
- pb.dsRefNum = rid.GetDSRefNum();
- pb.identity = identity;
-
- NilAddrBlock(&pb.serverHint);
-
- pb.aRecord = rid;
-
- pb.currentAttr = (Attribute*)this;
-
- pb.newAttr = (Attribute*)this;
-
-
- OSErr osErr = DirChangeAttributeValue((DirParamBlockPtr)&pb, false);
-
- return osErr;
-
- }
-
-
-
-
- //------------------------------------------------------------------------------------------
- #pragma segment Templates
- OSErr CAttribute::DeleteValue(CRecordID& rid,AddrBlock *serverHint, AuthIdentity identity)
- {
- DirDeleteAttributeValuePB pb;
-
- pb.dsRefNum = rid.GetDSRefNum();;
- pb.identity = identity;
-
- NilAddrBlock(&pb.serverHint);
-
- pb.aRecord = rid;
-
-
- pb.attr = (Attribute*)this;
-
-
- OSErr osErr = DirDeleteAttributeValue((DirParamBlockPtr)&pb, false);
-
- return osErr;
-
- }
-
-
- //------------------------------------------------------------------------------------------
- #pragma segment Templates
- OSErr CAttribute::VerifyValueCID(CRecordID& rid,AddrBlock *serverHint, AuthIdentity identity)
- {
- // Assumes the fBytes as a value.
-
- DirVerifyAttributeValuePB pb;
-
- pb.dsRefNum = rid.GetDSRefNum();;
- pb.identity = identity;
-
- NilAddrBlock(&pb.serverHint);
-
- pb.aRecord = rid;
-
-
- fCID.source = 0;
- fCID.seq = 0;
-
-
- pb.attr = (Attribute*)this;
-
-
- OSErr osErr = DirVerifyAttributeValue((DirParamBlockPtr)&pb, false);
-
- return osErr;
-
- }
-
-
- //------------------------------------------------------------------------------------------
- #pragma segment Templates
- static pascal Boolean __EachRecordCallBack(long /*clientData*/, const RecordID* /*localRID*/ )
- {
- return false;
- }
-
- //------------------------------------------------------------------------------------------
- #pragma segment Templates
- static pascal Boolean __EachAttributeCallBack(long /*clientData*/, const AttributeType* /*attrType*/, AccessMask /*myAttrAccMask*/)
- {
- return false;
- }
-
- //------------------------------------------------------------------------------------------
- #pragma segment Templates
- static pascal Boolean __EachAttributeValueCallBack(long clientData, const Attribute* attrValue)
- {
- CAttribute* attr = (CAttribute*)clientData;
-
- attr->fBytes = attrValue->value.bytes;
- attr->fDataLength = attrValue->value.dataLength;
- attr->fCID.source = attrValue->cid.source;
- attr->fCID.seq = attrValue->cid.seq;
- attr->fTag = attrValue->value.tag;
- return false; // we want only one value. (even if there are more)
-
- }
-
-
-
-
- //------------------------------------------------------------------------------------------
- #pragma segment Templates
- OSErr CAttribute::Read1Value(CRecordID& rid, void* valueBuf, long* valueBufSize, AuthIdentity idendtity)
- {
-
-
- DirLookupGetPB pb;
- memset(&pb,0,sizeof(DirLookupGetPB));
-
- pb.ioResult = 0;
- pb.ioCompletion = 0;
-
- pb.dsRefNum = rid.GetDSRefNum();
- pb.identity = idendtity;
-
-
- NilAddrBlock(&pb.serverHint);
-
-
- // 1. Assume ONE record id.
-
- RecordIDPtr aRIDPtr = rid;
- pb.aRecordList = &aRIDPtr;
- pb.recordIDCount = 1;
-
-
- // 2. Assumes ONE attribute type
-
- AttributeType* attrTypePtr = (AttributeType*)this->fAttributeType;
-
- pb.attrTypeList = &attrTypePtr;
- pb.attrTypeCount = 1;
-
-
- // 3. Allocate the buffer for toolbox
-
-
- Ptr dataBuf = NewPtr(*valueBufSize+kToolBoxAttrOverHead);
- OSErr osErr = MemError();
- if( osErr != 0 )
- return osErr;
-
- pb.getBuffer = dataBuf;
- pb.getBufferSize = *valueBufSize+kToolBoxAttrOverHead;
-
-
- // 4. Other Parameters
-
-
- pb.clientData = (long) this;
-
-
- if( fCID.source != 0 && fCID.seq != 0 ) {
- pb.includeStartingPoint = true; // when we want a value with particular cid
- (CAttribute&)pb.startingAttribute = *this; // copy
- } else {
- pb.includeStartingPoint = false;
- }
-
-
- pb.startingRecordIndex = 1;
- pb.startingAttrTypeIndex = 1;
-
- osErr = DirLookupGet((DirParamBlock*)&pb, false);
-
- if( osErr == 0 ) {
- DirLookupParsePB* parsePB = (DirLookupParsePB*)&pb;
- parsePB->eachRecordID = __EachRecordCallBack;
- parsePB->eachAttrType = __EachAttributeCallBack;
- parsePB->eachAttrValue = __EachAttributeValueCallBack;
-
- osErr = DirLookupParse((DirParamBlock*)&pb, false);
-
- }
-
-
- // 5. Copy results from toolbox buffer to client buffer.
-
-
- if( osErr == 0 && fDataLength != 0 && fDataLength <= *valueBufSize ) {
- *valueBufSize = fDataLength;
- BlockMove(fBytes,valueBuf,fDataLength);
- fBytes = 0; // so the destructor does not dispose
- } else {
- *valueBufSize = 0;
- }
-
- if (fDataLength == 0 && osErr == noErr)
- osErr = kOCENoSuchAttributeValue;
-
- DisposePtr(dataBuf);
- return osErr;
-
- }
-
-
-
-
- //------------------------------------------------------------------------------------
- #pragma segment CAttribute
- OSErr CAttribute::LookupValues(CRecordID* rid,CAttributeValueCallBack callBack,long clientData)
- {
- DirParamBlock pb;
- memset(&pb,0,sizeof(DirParamBlock));
-
-
- fValueCallBack = callBack;
- fClientData = clientData;
- fCallBackError = 0;
-
-
- // 1. Header
-
- pb.lookupGetPB.dsRefNum = rid->GetDSRefNum();
- pb.lookupGetPB.identity = gIdentity;
-
- // 2. Assume ONE record id.
-
-
- RecordIDPtr aRIDPtr = *rid;
- pb.lookupGetPB.aRecordList = &aRIDPtr;
- pb.lookupGetPB.recordIDCount = 1;
- pb.lookupGetPB.startingRecordIndex = 1;
-
-
- // 3. Assume ONE attribute type
-
- AttributeType* attrTypePtr = (AttributeType*)this->fAttributeType;
- pb.lookupGetPB.attrTypeList = &attrTypePtr;
- pb.lookupGetPB.attrTypeCount = 1;
- pb.lookupGetPB.startingAttrTypeIndex = 1;
-
-
- // 4. Other
- pb.header.clientData = (long)this;
- pb.lookupGetPB.includeStartingPoint = false;
-
-
-
- // 5. Buffer
- Ptr getBuff = NewPtr(kDefaultBuffSize);//•••
- if( getBuff == 0 )
- return MemError();
-
- pb.lookupGetPB.getBuffer = getBuff;
- pb.lookupGetPB.getBufferSize = kDefaultBuffSize;
-
-
- // 6. Call backs
- pb.lookupParsePB.eachRecordID = 0;
- pb.lookupParsePB.eachAttrType = 0;
- pb.lookupParsePB.eachAttrValue = __LookupValueCallBack;
-
-
- OSErr osErr = noErr;
- Boolean moreData = false;
- do {
-
- osErr = DirLookupGet(&pb,false);
- if (osErr==kOCEMoreData)
- moreData = true;
- else
- moreData = false;
-
- if (osErr==noErr || osErr==kOCEMoreData) {
- osErr = DirLookupParse(&pb,false);
- if (osErr!=noErr || fCallBackError != 0 )
- moreData = false; // this could be kOCEMoreAttrValue (buffer not big enough)
- // or kOCEMoreData (didn't have permission to read)
- }
- } while (moreData);
-
-
- DisposPtr(getBuff);
-
- if( osErr == 0 && fCallBackError != 0 )
- osErr = fCallBackError;
-
- return osErr;
- }
-
-
-
- //------------------------------------------------------------------------------------
- pascal Boolean CAttribute::__LookupValueCallBack(long clientData, const Attribute* attrValue)
- {
-
- // We have this bit of redirection so that the call back can return errors.
-
-
- Boolean haltEnumeration = false;
- OSErr osErr = 0;
-
- CAttribute* thisCAttribute = (CAttribute*) clientData;
- if( thisCAttribute->fValueCallBack != 0 )
- osErr = thisCAttribute->fValueCallBack(thisCAttribute->fClientData,attrValue);
-
-
- if(osErr != 0 ) {
- thisCAttribute->fCallBackError = osErr;
- haltEnumeration = true;
- }
-
-
- return haltEnumeration;
- }
-
-